﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace WebApplication6
{
    public partial class dingdan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string username = Convert.ToString(Session["yonghu"]);

            DataTable dt = SQLHelper.ExecuteDataTable("select * from t_gouwuche where username='" + username + "'");

            double zongjia = 0;
            string mingxi = "";
            string sql2 = "";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                double zongjia2 = Convert.ToDouble(row["zongjia"]);
                zongjia = zongjia + zongjia2;
                mingxi = mingxi+  "商品名:"+ Convert.ToString(row["spname"])+",购买数量："
                    +Convert.ToInt32(row["num"])+",单价："+Convert.ToDouble(row["jiage"])
                    +"￥,总价："+Convert.ToDouble(row["zongjia"])+"￥<br/>";
                
                //更新商品的购买销量
                 sql2 = " update t_product set xiaoliang = xiaoliang +  " + Convert.ToInt32(row["num"])
                    + " where id=" + Convert.ToInt32(row["spid"]);
                SQLHelper.ExecuteNonQuery(sql2);

            }

            

           DateTime t1 = new DateTime(); 
           t1 = Convert.ToDateTime(("2013-01-01 08:00:00"));
            string orderid = ((System.DateTime.Now.Ticks - t1.Ticks) / 10000).ToString();

            string sql = @"  insert into t_dingdan(username,truename,address,phone,orderid,zhuangtai,createtime,mingxi,zongjia)
            
             values('"+username+"','"+truename.Text+"','"+address.Text+"','"+phone.Text+"','"
                      +orderid+"','"+"未处理"+"','"+Convert.ToString(DateTime.Now)+"','"+mingxi+"',"+zongjia+") ";
            SQLHelper.ExecuteNonQuery(sql);


            sql = " delete from t_gouwuche where username='" + username + "'";

            SQLHelper.ExecuteNonQuery(sql);

            Response.Write("<script language=javascript>alert('订单生成成功');window.location.href='dingdanlist.aspx';</script>");
           

        }
    }
}
